//
//  ZDBFeedbackListViC.m
//  ZDB
//
//  Created by Kk on 2020/3/29.
//  Copyright © 2020 ZDB. All rights reserved.
//

#import "ZDBFeedbackListViC.h"
#import "ZDBFeedbackTableViewCell.h"
#import "FeedbackModel.h"
#import "ZDBFeedbackInfoVC.h"
#import "HUPhotoBrowser.h"
@interface ZDBFeedbackListViC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic,strong) NoNetworkView * workView;
@end

@implementation ZDBFeedbackListViC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的反馈";
    [self.view addSubview:self.tableView];
    WeakSelf
    // 上拉加载
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page =1;
        [weakSelf getFeedbackList];
    }];
    [header beginRefreshing];
    self.tableView.mj_header = header;
    
    MJRefreshBackStateFooter * foot = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        self.page ++;
        [weakSelf getFeedbackList];
    }];
    self.tableView.mj_footer = foot;
}
#pragma 获取反馈列表
- (void)getFeedbackList
{
    WeakSelf
    NSString  * feedbackList = [[YINetWorkAPIGenerate sharedInstance] APINomark:@"feedbackList"];
    [[SmileHttpTool sharedInstance] POST:feedbackList parameters:@{@"type":@"",@"pageNum":@(self.page),@"pageSize":@(10)} origin:NO success:^(NSInteger statusCode, NSString *message, id responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        NSDictionary * dic = responseObject;
        if (!kDictIsEmpty(dic)) {
            NSInteger  pages = [dic[@"pages"] integerValue];//一共多少条数据
            NSArray * list = dic[@"list"];//
            if (weakSelf.page>pages)
            {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }else
            {
                if (weakSelf.page > 1)
                {
                    [weakSelf.dataSource addObjectsFromArray:[FeedbackModel mj_objectArrayWithKeyValuesArray:list]];
                    
                }else
                {
                    self.dataSource = [FeedbackModel mj_objectArrayWithKeyValuesArray:list];
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self placeholderViewWithFrame:self.tableView.frame NoNetwork:NO];
            });
            [self.tableView reloadData];
        }else
        {
            
            [JMNotifyView showNotify:message isSuccessful:NO];
        }
        
    } failure:^(id responseObject, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}
- (void)placeholderViewWithFrame:(CGRect)frame NoNetwork:(BOOL)NoNetwork
{
    if (self.dataSource.count == 0)
    {
        [_workView removeFromSuperview];
        _workView = [[NoNetworkView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) NoNetwork:NoNetwork];
        [self.tableView addSubview:_workView];
        //_workView.centerY = self.collectionView.centerY-100;
    }else
    {
        [_workView dissmiss];
    }
    
}
- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}
#pragma  mark -----懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight  - kStatusBarAndNavigationBarHeight)style:UITableViewStyleGrouped];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorColor = CELLCOLOR;
        _tableView.delegate = self;
        _tableView.dataSource=self;
        _tableView.backgroundColor =  VIEWBGCOLOR;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 0.0001)];
        [_tableView registerNib:[UINib nibWithNibName:@"ZDBFeedbackTableViewCell" bundle:nil]  forCellReuseIdentifier:@"ZDBFeedbackTableViewCell"];
    
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
      FeedbackModel * model = self.dataSource[indexPath.row];
    if (model.feedbackImageList.count>0) {
         return 110;
    }else
    {
         return 80;
    }
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDBFeedbackTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"ZDBFeedbackTableViewCell" forIndexPath:indexPath];
    
    cell.model = _dataSource[indexPath.row];
    WeakSelf
    [cell.lookImgBtn add_BtnClickHandler:^(NSInteger tag) {
        FeedbackModel * model = weakSelf.dataSource[indexPath.row];
        NSMutableArray * images = [NSMutableArray new];
        for (NSDictionary * dic in model.feedbackImageList)
        {
            [images addObject:dic[@"url"]];
        }
        [HUPhotoBrowser showFromImageView:cell.imageView withURLStrings:images atIndex:0];
         
    }];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FeedbackModel * model = self.dataSource[indexPath.row];
    ZDBFeedbackInfoVC * feedbackInfoVC = [[ZDBFeedbackInfoVC alloc] init];
    feedbackInfoVC.title = @"反馈信息";
    feedbackInfoVC.model = model;
    [self.navigationController pushViewController:feedbackInfoVC animated:YES];
}

@end
